﻿using System;

namespace UmengAnalyticsNet
{
    public class OS
    {
        private static Version os = null;
        public static OSType GetOSVersion()
        {
            if (os == null)
            {
                try
                {
                    var version = Environment.OSVersion;
                    os = version.Version;
                }
                catch (Exception)
                {
                    //
                }
            }

            if (os == null)
            {
                os = Environment.OSVersion.Version;
            }

            OSType osType = OSType.None;
            if (os.Major == 5)
            {
                if (os.Minor == 0)
                {
                    osType = OSType.Windows2000;
                }
                else if (os.Major == 1)
                {
                    osType = OSType.WinXp;
                }
                else if (os.Major == 2)
                {
                    osType = OSType.Windows2003;
                }

            }
            else if (os.Major == 6)
            {
                if (os.Minor == 0)
                {
                    osType = OSType.Vista;
                }
                else if (os.Minor == 1)
                {
                    osType = OSType.Win7;
                }
                else if (os.Minor == 2)
                {
                    osType = OSType.Win8;
                }
                else if (os.Minor == 3)
                {
                    osType = OSType.Win81;
                }
            }
            else if (os.Major == 10)
            {
                osType = OSType.Win10;
            }

            return osType;
        }

        public static string GetOSString()
        {
            var result = "unknown";
            var osType = GetOSVersion();
            switch (osType)
            {
                case OSType.Windows2000:
                case OSType.Windows2003:
                case OSType.WinXp:
                    result = "xp";
                    break;
                case OSType.Vista:
                case OSType.Win7:
                    result = "7";
                    break;
                case OSType.Win8:
                    result = "8";
                    break;
                case OSType.Win81:
                    result = "81";
                    break;
                case OSType.Win10:
                    result = "10";
                    break;
            }

            return result;
        }
    }

    public enum OSType
    {
        None,
        Windows2000,
        Windows2003,
        WinXp,
        Vista,
        Win7,
        Win8,
        Win81,
        Win10
    }
}